import java.io.*;

/**
 * @projectName: java-daily-questions
 * @package: PACKAGE_NAME
 * @className: FileRecover
 * @author: ZM
 * @description: 文件恢复
 * @date: 2021/8/18 11:06
 * @version: 1.0
 */
public class FileRecover {

    public static void recover(String path){
        File file = new File(path);
        String parent = file.getParent();
        String name = file.getName();
        String dst = parent + "\\" + name.replace("1", "");
        File result = new File(dst);
        InputStream is = null;
        OutputStream os = null;
        try {
            if (!result.exists()) {
                result.createNewFile();
            }
            is = new FileInputStream(file);
            os = new FileOutputStream(result);

            int length;
            byte[] buffer = new byte[1024];
            while((length = is.read(buffer)) != -1) {
                for (int i = 0; i < buffer.length; i++) {
                    buffer[i] = (byte) (255 - buffer[i]);
                }
                os.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                // 关闭资源 先开后关
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }




    }

    public static void main(String[] args){
        String path = "D:\\zhangmiao\\work\\idea-workspace\\java-daily-questions\\files\\日落大道1.mp3";
        recover(path);
    }
}
